<?php
require_once dirname(__FILE__) . '/common/common.inc.php';

if (isset($_COOKIE[USER])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
}

$template = MiscUtils::getParam('t', 'signin');
$base = MiscUtils::getParam('b', 'base');
$options = (MiscUtils::decryptParam('opts', NULL) == NULL) ? json_encode(array()) : $options;

$smarty->assign('user', json_encode(array()));
$smarty->assign('date', time());
$smarty->assign('options', $options);
$smarty->assign('message', json_encode(array()));
$smarty->assign('menu', new stdClass());

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[USER]);
  setcookie(USER, '', time() - 1, PATH, DOMAIN);
  $smarty->assign('template', 'signin');
  $smarty->display("gui/basesignin.tpl");
  return; /* SignOut */
}

if (isset($_SESSION[USER])) {
  $message = array();
  
  //If login already, skip login page. Will go to home page
  if ($_SESSION[USER]->type == USER_TYPE_ADMIN && $template == 'signin') {
    $template = 'client';
  } else if ($_SESSION[USER]->type == USER_TYPE_USER && $template == 'signin') {
    $template = 'client';
  }
  $smarty->assign('u', $_SESSION[USER]);
  $smarty->assign('template', $template);
  $smarty->assign('user', json_encode($_SESSION[USER]));
  $smarty->assign('date', time());
  $smarty->assign('options', urldecode($options));
  
  setData($template, $message);
  $smarty->display("gui/$base.tpl");
  return;
} else {
  $smarty->assign('template', 'signin');
  $smarty->display("gui/basesignin.tpl");
  return; /* SignIn Required */
}

function setData($template, $message) {
  switch ($template) {
    case 'account':
      p_account($message);
      break;
    case 'users':
      p_users($message);
      break;
    case 'user':
      p_user($message);
      break;
    case 'templates':
      p_templates($message);
      break;
    case 'template':
      p_template($message);
      break;
    case 'clients':
      p_clients($message);
      break;
    case 'client':
      p_client($message);
      break;
    case 'visas':
      p_visas($message);
      break;
    case 'visabatch':
      p_visabatch($message);
      break;
    case 'duevisas':
      p_duevisas($message);
      break;
    case 'dueschools':
      p_dueschools($message);
      break;
    case 'system':
      p_system($message);
      break;
    case 'contract1':
      p_contract1($message);
      break;
    default:
      break;
  }
}

function p_account($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 4;
  $smarty->assign('menu', $menu);
  
  $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, "id = " . $_SESSION[USER]->id, NULL, NULL, NULL, NULL);
  $smarty->assign('message', json_encode($message));
}

function p_users($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, NULL, NULL, NULL, NULL, NULL);
  $smarty->assign('message', json_encode($message));
}

function p_user($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, "id = $id", NULL, NULL, NULL, NULL);
    $smarty->assign('message', json_encode($message));
  }
}

function p_templates($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_template', NULL, NULL, NULL, NULL, NULL, NULL);
  $smarty->assign('message', json_encode($message));
}

function p_template($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_template', NULL, "id = $id", NULL, NULL, NULL, NULL);
    $smarty->assign('message', json_encode($message));
  }
}

function p_clients($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 1;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $cond = NULL;
  if ($_SESSION[USER]->type != USER_TYPE_ADMIN) {
    $cond = 't.staff = \'' . $_SESSION[USER]->account . '\'';
  }
  $message[0] = MiscUtils::db(getMyPdo(), 'DISTINCT t.id, t.*, t1.visa_date_expiry_new', 'pz_client', array(array('pz_visa', 't.latest_visa_id = t1.id')), $cond, 't.id', 'DESC', START, SIZE);
  
  $message['nationality'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.nationality', 'pz_client', NULL, "t.nationality != ''", 't.nationality', 'ASC', NULL, NULL);
  $message['school'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.school_name', 'pz_school', NULL, "t.school_name != ''", 't.school_name', 'ASC', NULL, NULL);
  $message['staff'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.staff', 'pz_client', NULL, "t.staff != ''", 't.staff', 'ASC', NULL, NULL);
  $message['agent'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.agent', 'pz_client', NULL, "t.agent != ''", 't.agent', 'ASC', NULL, NULL);
  $message['visa_type'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.visa_appl_type', 'pz_visa', NULL, "t.visa_appl_type != ''", 't.visa_appl_type', 'ASC', NULL, NULL);
  $smarty->assign('message', json_encode($message));
}

function p_client($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 1;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $menu->b = 2;
    $smarty->assign('menu', $menu);
    $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_client', NULL, "id = $id", NULL, NULL, NULL, NULL);
  }
  
  $message['m'] = MiscUtils::getParam('m', 'visa');
  $message['u'] = MiscUtils::db(getMyPdo(), '*', 'pz_user', NULL, NULL, NULL, NULL, NULL, NULL);
  $smarty->assign('message', json_encode($message));
}

function p_visas($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 2;
  $menu->b = 2;
  $smarty->assign('menu', $menu);
  
  $cond = NULL;
  if ($_SESSION[USER]->type != USER_TYPE_ADMIN) {
    $cond = 't1.staff = \'' . $_SESSION[USER]->account . '\'';
  }
  $message[0] = MiscUtils::db(getMyPdo(), 'DISTINCT t.id, t.*, t1.name_first, t1.name_family, t1.date_birth, t1.pass_num, t1.staff', 'pz_visa', array(array('pz_client', 't.client_id = t1.id')), $cond, 't.visa_appl_date_submit', 'ASC', START, SIZE);
  
  $message['nationality'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.nationality', 'pz_client', NULL, "t.nationality != ''", 't.nationality', 'ASC', NULL, NULL);
  $message['school'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.school_name', 'pz_school', NULL, "t.school_name != ''", 't.school_name', 'ASC', NULL, NULL);
  $message['staff'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.staff', 'pz_client', NULL, "t.staff != ''", 't.staff', 'ASC', NULL, NULL);
  $message['agent'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.agent', 'pz_client', NULL, "t.agent != ''", 't.agent', 'ASC', NULL, NULL);
  $message['visa_type'] = MiscUtils::db(getMyPdo(), 'DISTINCT t.visa_appl_type', 'pz_visa', NULL, "t.visa_appl_type != ''", 't.visa_appl_type', 'ASC', NULL, NULL);
  $smarty->assign('message', json_encode($message));
}

function p_visabatch($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 2;
  $menu->b = 1;
  $smarty->assign('menu', $menu);
  
  $cond = NULL;
  if ($_SESSION[USER]->type != USER_TYPE_ADMIN) {
    $cond = 't1.staff = \'' . $_SESSION[USER]->account . '\'';
  }
  $message[0] = MiscUtils::db(getMyPdo(), 'DISTINCT t.id, t.*, t1.name_first, t1.name_family, t1.date_birth, t1.pass_num, t1.staff', 'pz_visa', array(array('pz_client', 't.client_id = t1.id')), $cond, 't.id', 'DESC', START, SIZE);
  $smarty->assign('message', json_encode($message));
}

function p_duevisas($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 3;
  $menu->b = 1;
  $smarty->assign('menu', $menu);

  $cond = '';
  if ($_SESSION[USER]->type != USER_TYPE_ADMIN) {
    $cond = ' AND t1.staff = \'' . $_SESSION[USER]->account . '\'';
  }
  $message[0] = MiscUtils::db(getMyPdo(), 'DISTINCT t.id, t.*, t1.name_first, t1.name_family, t1.date_birth, t1.pass_num, t1.staff, t1.agent, t1.phone1, t1.phone2, t1.phone3, t1.email', 'pz_visa', array(array('pz_client', 't.client_id = t1.id')), 't.date_reminder != -1 AND t.date_reminder <= ' . time() . ' AND t.visa_date_expiry_new != 0 AND t.visa_date_expiry_new <= ' . (time() + 86400 * 60) . $cond, "t.visa_date_expiry_new", 'ASC', START, SIZE);
  $smarty->assign('message', json_encode($message));
}

function p_dueschools($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 3;
  $menu->b = 2;
  $smarty->assign('menu', $menu);

  $cond = '';
  if ($_SESSION[USER]->type != USER_TYPE_ADMIN) {
    $cond = ' AND t1.staff = \'' . $_SESSION[USER]->account . '\'';
  }
  $message[0] = MiscUtils::db(getMyPdo(), 'DISTINCT t.id, t.*, t1.name_first, t1.name_family, t1.date_birth, t1.pass_num, t1.staff, t1.agent, t1.phone1, t1.phone2, t1.phone3, t1.email', 'pz_school', array(array('pz_client', 't.client_id = t1.id')), 't.date_reminder != -1 AND t.date_reminder <= ' . time() . ' AND t.next_payment_date != 0 AND t.next_payment_date <= ' . (time() + 86400 * 60) . $cond, "t.next_payment_date", 'ASC', START, SIZE);
  $smarty->assign('message', json_encode($message));
}

function p_system($message) {
  global $smarty;

  $menu = new stdClass();
  $menu->a = 4;
  $menu->b = 3;
  $smarty->assign('menu', $menu);
  
  $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_system', NULL, NULL, NULL, NULL, 1, 1);
  $smarty->assign('message', json_encode($message));
}

function p_contract1($message) {
  global $smarty, $base;

  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $menu->b = 2;
    $smarty->assign('menu', $menu);
    $message[0] = MiscUtils::db(getMyPdo(), '*', 'pz_client', NULL, "id = $id", NULL, NULL, NULL, NULL);
  }
  
  $message['sys'] = MiscUtils::db(getMyPdo(), '*', 'pz_system', NULL, NULL, NULL, NULL, 1, 1);
  $smarty->assign('message', json_encode($message));
  
  $base = 'baseblank';
}

function getMyPdo() {
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ("mysql:host=$host", $login, $pass);
    $myPdo->exec("USE $db");
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $myPdo;
  } catch (PDOException $e) {
    echo ERROR_SIGN . $e->getMessage();
  }
  return NULL;
}
?>